<?php

class PostController extends ControllerBase
{
    private function _getDbJson()
    {
        return BASE_PATH . '/_site/' . $this->siteid . '/post/db.json';
    }

    public function indexAction()
    {
        $this->tag->prependTitle("文章管理 - ");
        $this->assets->addCss("/static/common/css/icheck/skins/minimal/blue.css");
        $this->assets->addCss("/css/admin.css");
        $this->assets->addJs("/js/split-pane.js");
        $this->assets->addJs("/static/common/js/icheck/icheck.min.js");
        $this->assets->addJs("/static/post/js/list.js");
        if (file_exists($this->_getDbJson())) {
            $string = file_get_contents($this->_getDbJson());
            $config = json_decode($string, true);
        } else {
            $config = [];
        }
        $file = Common::getDirContent(BASE_PATH . '/_site/post/' . $this->siteid);
        $this->view->setVars(
            [
                'file'      => $file,
                'config'    => $config,
                'crumbData' => [
                    [
                        'name' => $this->view->currentSite[$this->siteid]['name'],
                        'url'  => '/' . $this->siteid . '/dir/index',
                    ],
                    [
                        'name' => '内容管理',
                        'url'  => 'javascript:;',
                        'curr' => 1,
                    ],
                ],
            ]
        );
    }

    public function createAction()
    {
        $title = $this->request->getPost('title');
        $path  = $this->request->getPost('path');
        $md    = "title:" . $title . PHP_EOL;
        $md .= "intro:intro" . PHP_EOL;
        $md .= PHP_EOL;
        $md .= "-------------------------------------";
        $md .= PHP_EOL;
        $md .= 'content';
        file_put_contents(BASE_PATH . '/_site/' . $this->siteid . '/post/' . $path . '.md', $md);
        if (file_exists($this->_getDbJson())) {
            $db = file_get_contents($this->_getDbJson());
            $db = json_decode($db, true);
        } else {
            $db = [];
        }
        $db[]   = ['title' => $title, 'uid' => $this->auth['id'], 'path' => $path, 'created_time' => time()];
        $string = json_encode($db, JSON_UNESCAPED_UNICODE);
        file_put_contents($this->_getDbJson(), $string);
        $data = ['title' => $title, 'path' => $path, 'siteid' => $this->siteid];
        $json = ['code' => 0, 'msg' => null, 'data' => $data];
        $this->response->setJsonContent($json);
        return $this->response;
    }

    public function editAction()
    {

        $path = $this->request->getQuery('path');
        $this->assets->addCss("/editor/css/editormd.css");
        $this->assets->addJs("/editor/js/editormd.min.js");
        $this->assets->addJs("/static/post/js/edit.js");
        $file  = file_get_contents(BASE_PATH . '/_site/' . $this->siteid . '/post/' . $path . '.md');
        $arr   = explode('-------------------------------------', $file);
        $first = explode(PHP_EOL, $arr[0]);
        //title
        $title = explode(':', $first[0]);
        //intro
        $intro = explode(':', $first[1]);
        $this->tag->prependTitle($title[1] . ' - 编辑文章 - ');

        $this->view->setVars(
            [
                $title[0]   => $title[1],
                $intro[0]   => $intro[1],
                'content'   => $arr[1],
                'path'      => $path,
                'crumbData' => [
                    [
                        'name' => $this->view->currentSite[$this->siteid]['name'],
                        'url'  => '/' . $this->siteid . '/dir/index',
                    ],
                    [
                        'name' => '内容管理',
                        'url'  => '/' . $this->siteid . '/post/index',
                    ],
                    [
                        'name' => $title[1],
                        'url'  => 'javascript:;',
                        'curr' => 1,
                    ],
                ],
            ]
        );
    }

    public function saveAction()
    {
        $path    = $this->request->getPost('path');
        $title   = $this->request->getPost('title');
        $intro   = $this->request->getPost('intro');
        $content = $this->request->getPost('content');
        $md      = "title:" . $title . PHP_EOL;
        $md .= "intro:" . $intro . PHP_EOL;
        $md .= PHP_EOL;
        $md .= "-------------------------------------";
        $md .= PHP_EOL;
        $md .= $content;
        file_put_contents(BASE_PATH . '/_site/' . $this->siteid . '/post/' . $path . '.md', $md);
        $json = ['code' => 0, 'msg' => null];
        $this->response->setJsonContent($json);
        return $this->response;
    }

    public function deleteAction()
    {
        $json = ['code' => 0, 'msg' => null];
        $this->response->setJsonContent($json);
        return $this->response;
    }

}
